#include"Task.hpp"
#include"RingQueue.hpp"
#include<time.h>
#include<pthread.h>
#include<memory>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
const char* ops="+-*/%";

void* consumerRoutine(void* args)
{
    RingQueue<Task>* rq=static_cast<RingQueue<Task>*>(args);
    while (true)
    {
        Task t;
        rq->pop(&t);
        t();
        cout<<"consumer done:"<<t.formatRet()<<endl;
        sleep(1);
    }
    

}
void* productorRoutine(void* args)
{
    RingQueue<Task>* rq=static_cast<RingQueue<Task>*>(args);
    while (true)
    {
        //int data=rand()%10+1;
        int x=rand()%100;
        int y=rand()%100;
        char op=ops[(x+y)%5];
        Task t(x,y,op);
        rq->push(t);
        cout<<"product done:"<<t.formatArg()<<endl;
        
    }
    

}

int main()
{
    srand(time(nullptr)^getpid());
    RingQueue<Task>* rq=new RingQueue<Task>();
    //单生产 单消费
    pthread_t c,p;
    pthread_create(&c,nullptr,consumerRoutine,rq);
    pthread_create(&p,nullptr,productorRoutine,rq);//创建线程 pthread_create(*返回线程id，设置线程属性，回调函数，回调函数参数（共享资源）)
    //线程等待
    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    delete rq;
    return 0;
}